// @algorithm @lc id=208 lang=cpp 
// @title implement-trie-prefix-tree
class Trie {
	bool end;
	vector<Trie*> dict;
public:
    /** Initialize your data structure here. */
    Trie() : end(false), dict(26, NULL)
	{
    }
    
    /** Inserts a word into the trie. */
    void insert(string word) {
		auto node = this;
		for(auto &c : word){
			auto it = node->dict.begin()+c-'a';
			if(!*it)*it = new Trie();
			node = *it;
		}
		node->end = true;
    }
	Trie * match(string word)
	{
		auto node = this;
		for(auto &c : word){
			auto it = node->dict.begin()+c-'a';
			if(!*it) return NULL;
			node = *it;
		}
		return node;
	}
    
    /** Returns if the word is in the trie. */
    bool search(string word) {
		auto node = match(word);
		return node ? node->end : false;
    }
    
    /** Returns if there is any word in the trie that starts with the given prefix. */
    bool startsWith(string prefix) {
		return match(prefix);
    }
};

/**
 * Your Trie object will be instantiated and called as such:
 * Trie* obj = new Trie();
 * obj->insert(word);
 * bool param_2 = obj->search(word);
 * bool param_3 = obj->startsWith(prefix);
 */